import java.awt.Graphics;

public class World implements Drawable{

	private final static World world = new World();
	private Map map;

	private World()
	{

	}

	public static World getInstance()
	{
		return world;
	}

	public Map getMap()
	{
		return this.map;
	}

	public void setMap(Map m){
		//map.reset();
		map = m;
	}

	public void movePlayer(int dir) {
		map.movePlayer(dir);
	}

	public void updateText() {
		//System.out.println(map.player.getX() + " "  + map.player.getY() + " oldDir: " + OurApplet.oldDir);
		if (!map.player.getMoving()) {
			switch (map.player.getFacing()) {
			case 1 :
				for (NPC npc : map.getNPCs()) {
					if (npc.getX() == map.player.getX() + 1 && npc.getY() == map.player.getY() && !npc.getMoving()) {
						npc.updateText();
					}
				}
			case 2 :
				for (NPC npc : map.getNPCs()) {
					if (npc.getX() == map.player.getX() && npc.getY() == map.player.getY() - 1 && !npc.getMoving()) {
						npc.updateText();
					}
				}
			case 3 :
				for (NPC npc : map.getNPCs()) {
					if (npc.getX() == map.player.getX() - 1 && npc.getY() == map.player.getY() && !npc.getMoving()) {
						npc.updateText();
					}
				}
			case 4 :
				for (NPC npc : map.getNPCs()) {
					if (npc.getX() == map.player.getX() && npc.getY() == map.player.getY() + 1 && !npc.getMoving()) {
						npc.updateText();
					}
				}
			}
		}
	}


	@Override
	public void draw(Graphics g) {
		map.draw(g);
		for(NPC n : map.getNPCs()){
			if (n.getBubbleList().get(n.getCurrentLine()) != null){
				OurApplet.talking = true;
				n.getBubbleList().get(n.getCurrentLine()).draw(g);
				break; //I know it's nasty, but we needed to break this loop if OurApplet.talking is true. otherwise, we could end up setting it back to false if we have multiple NPCs. -Brandon
			} else if (OurApplet.talking == true) {
				OurApplet.talking = false;
			}
		}
	}
}
